﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Web;
using Medianamik.Core;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.Sugar;
using Medianamik.UI.Web.MVP.Presentation;

namespace Medianamik.UI.Web.MVP
{
    public class BreadcrumbItem
    {
        public string Url { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return String.IsNullOrEmpty(Url) ? Name : string.Format("<a href=\"{0}\">{1}</a>", Url, Name);
        }
    }

    public class BreadcrumbBuilder
    {
        private readonly StringBuilder _sb;
        private readonly List<BreadcrumbItem> _items;

        public BreadcrumbBuilder()
        {
            _sb = new StringBuilder();
            _items = new List<BreadcrumbItem>();
        }

        private bool HasItems { get { return _items.Count > 0; } }
        private void BeginBreadcrumb()
        {
            Clear();
            _sb.Append("<div class=\"FilAriane\"><ul id=\"FilAriane\" class=\"FilAriane\">");
        }
        private void EndBreadcrumb()
        {
            _sb.Append("</ul><br class=\"clearer\"></div>");
        }

        public ReadOnlyCollection<BreadcrumbItem> Items
        {
            get { return _items.ToList().AsReadOnly(); }
        }

        public void AppendItem(BreadcrumbItem item)
        {
            if(item == null)
                throw new ArgumentNullException("item");

            _items.Add(item);
        }

        public void AppendRawUrl(string rawUrl, bool appendParents)
        {
            if(rawUrl == null)
                throw new ArgumentNullException("rawUrl");

            if (rawUrl.IndexOf('?') != -1)
                rawUrl = rawUrl.Substring(0, rawUrl.IndexOf('?'));

            var node = SiteMap.Provider.FindSiteMapNode(rawUrl);
            if(node == null)
                throw new ApplicationException("Page not found");

            var item = new BreadcrumbItem() { Name = node.Title, Url = node.Url };

            if(appendParents)
            {
                var parents = new List<SiteMapNode>();
                var parentNode = node.ParentNode;
                while (parentNode != null && parentNode != SiteMap.RootNode)
                {
                    parents.Add(parentNode);
                    parentNode = parentNode.ParentNode;
                }
                parents.Reverse();
                foreach(var n in parents)
                {
                    _items.Add(new BreadcrumbItem() { Name = n.Title, Url = n.Url });
                }
            }

            _items.Add(item);
        }

        public void AppendNode(string url, string paramName, Guid nodeId, bool appendParents)
        {
            var node = NodeManager.GetNode(nodeId);

            if (node == null)
                throw new ApplicationException("Node not found");

            if (appendParents)
            {
                var parents = node.Breadcrumbs;
                foreach (var n in parents)
                {
                    _items.Add(new BreadcrumbItem() { Name = n.Name, Url = url.AddParam(paramName, n.ID) });
                }
            }

            _items.Add(new BreadcrumbItem()
                           {
                               Name = node.DisplayName,
                               Url = url.AddParam(paramName, node.ID)
                           });
        }

        public void Clear()
        {
            _sb.Length = 0;
        }

        public override string ToString()
        {
            if (!HasItems) return string.Empty;

            BeginBreadcrumb();

            for (int i = 0; i < _items.Count; i++)
            {
                if (i == _items.Count - 1)
                {
                    _sb.Append(FormatLastItem(_items[i]));
                    break;
                }
                _sb.AppendFormat("<li>{0}</li>", _items[i]);
            }

            EndBreadcrumb();

            return _sb.ToString();
        }

        private string FormatLastItem(BreadcrumbItem item)
        {
            return string.Format("<li class=\"DernierItem\">{0}</li>", item.Name);
        }
    }
}
